﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CsharpDdvancedProgramming_12_2_2CreateWPFDependencyProperty
{
    public class MyBook : DependencyObject //依赖属性必须派生自DependencyObject或其子类
    {

        //定义一个依赖属性
        public static readonly DependencyProperty BookNameProperty;

        static MyBook()
        {
            BookNameProperty = DependencyProperty.Register("BookName",
                                                       typeof(string),
                                                       typeof(MyBook),
                                                       new PropertyMetadata("No Name", new PropertyChangedCallback(BookNameChangedCallback), //属性变更回调
                                                       new CoerceValueCallback(BookNameCoerceCallback)),                                    //强制值回调
                                                       new ValidateValueCallback(BookNameValidatedCallback));                               //属性验证回调
        }
        //当属性值发生变化时，简单地面输出窗口中输出属性值
        private static void BookNameChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            Debug.WriteLine(e.OldValue + " " + e.NewValue);
        }
        //定义强制值回调，使属性值限定在8个字符以内，多余的将被裁切
        private static  object BookNameCoerceCallback(DependencyObject obj, object o)
        {
            string s = o as string;
            if (s.Length > 8)
                s = s.Substring(0, 8);
            return s;
        }
        //验证属性值是否为空，如果为空，则返回false
        private static bool BookNameValidatedCallback(object value)
        {
            return value != null;
        }
        //为依赖属性定义标准属性的包装
        public string BookName
        {
            get { return (string)GetValue(BookNameProperty); }
            set {  SetValue(BookNameProperty,value); }
        }
    }
}
